package com.chushouya.order.util;

import com.chushouya.order.constants.KefuOrderActionEnum;
import com.chushouya.order.constants.OrderStatusEnum;
import com.chushouya.order.dto.admin.action.OrderAction;
import com.google.common.collect.Maps;
import com.general.framework.core.lang.Lists;
import lombok.extern.slf4j.Slf4j;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Slf4j
public class OrderActionUtil {

    /**
     * 客服订单各状态下，可执行的操作列表
     */
    private static Map<Integer, List<OrderAction>> orderActionMap = Maps.newHashMap();

    private OrderActionUtil() {
    }

    private static void initOrderAction() {
//        log.debug("----------客服订单操作----------");
        Lists.asList(OrderStatusEnum.values()).forEach(orderStatus -> {
//            log.debug("订单状态:{},{}", orderStatus.value(), orderStatus.named());
            final List<OrderAction> actionList = getActionList(orderStatus);
            if (Lists.isNotEmpty(actionList)) {
                orderActionMap.put(orderStatus.value(), actionList);
            }
//            log.debug("-----------------------------");
        });
    }

    private static List<OrderAction> getActionList(OrderStatusEnum orderStatus) {
        List<OrderAction> optionList = Lists.newArrayList();
        Lists.asList(KefuOrderActionEnum.values()).forEach(actionEnum -> {
            if (actionEnum.fromOrderStatus().contains(orderStatus) && !actionEnum.moreCondition()) {
                optionList.add(new OrderAction(actionEnum.getActionCode(), actionEnum.getActionName(), actionEnum.getActionText()));
            }
        });
        return optionList;
    }

    public static List<OrderAction> orderActions(Integer orderStatus) {
        if (orderActionMap.size() == 0) {
            initOrderAction();
        }
        final List<OrderAction> currentStatusActions = orderActionMap.get(orderStatus);
        if (Lists.isEmpty(currentStatusActions)) {
            return Lists.newArrayList();
        }
        return new ArrayList<>(currentStatusActions);
    }
}
